--
-- Author: shunguo.chen
-- Date: 2018-06-27 20:15:16
--
-- BlessRewardView
--

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BlessRewardScene

local BLESS_NUM_STR = L("lua_code_text_121")
local BLESS_TIP_STR = L("lua_code_text_82")

function ClassRef:init()
	self._ui.Button_cancel:addClickEventListener(function()
			executeFunc(self._onCloseView, self)
		end)
	self._ui.Button_wish:addTouchEventListener(function(sender,eventType)
			if eventType == ccui.TouchEventType.ended then
				if self._blessTimes > 0 then
					executeFunc(self._onOperate, "reqBless", self._data)
					executeFunc(self._onCloseView, self)
				else
					display.pushToast(BLESS_TIP_STR)
					display.setBtnGray(self._ui.Button_wish, true)
				end
			elseif eventType == ccui.TouchEventType.move then

			else
				display.setBtnGray(self._ui.Button_wish, self._blessTimes <= 0)
			end
		end)

	local panel = self._ui.Panel
	local renders = {0,0,0}
	local nameNodes = {0,0,0}
	for i=1,3 do
		local node = panel:getChildByName("Panel_" .. i)
		local render = IconRender.create(node:getChildByName("icon"))
		render:setTipEnable(true)
		render:setTipUnShowOrigin(true)
		renders[i] = render
		nameNodes[i] = node:getChildByName("name")
	end
	self._renders = renders
	self._nameNodes = nameNodes

	self._tltText = self._ui["Panel/tlt_1"]
	self._numText = self._ui["Panel_bom/tips"]

	local con = self._ui["Panel/icon_1"]
	self._icon = con:getChildByName("icon")
	self._guildCon = con:getChildByName("guild_icon")
	self._guideIcon1 = self._guildCon:getChildByName("icon_u")
	self._guideIcon2 = self._guildCon:getChildByName("icon_b")
end

-- ZoneInfo
function ClassRef:refresh(zoneInfo)
	self._data = zoneInfo
	self._tltText:setString(zoneInfo:getName())
	ccui.Helper:doLayout(self._tltText)

	self._icon:loadTexture(zoneInfo:getIcon())
	self._guildCon:setVisible(false)
end

function ClassRef:refreshBlessTimes(blessTimes)
	self._blessTimes = blessTimes
	display.setBtnGray(self._ui.Button_wish, self._blessTimes <= 0)
	self._numText:setString(string.format(BLESS_NUM_STR, blessTimes))
end

-- rewards = {Daibi, Fame, Item}
function ClassRef:refreshRewards(rewards)
	for i,render in ipairs(self._renders) do
		local reward = rewards[i]
		render:setVisible(reward ~= nil)
		self._nameNodes[i]:setVisible(reward ~= nil)
		if reward then
			render:setData(reward)
			self._nameNodes[i]:setString(reward:getName())
		end
	end
end

return ClassRef
